package com.example.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.dao.IDepartmentDao;
import com.example.entity.Department;
import com.example.entity.delay.CallCdr;
import com.example.entity.department.DepartmentOneBO;
import com.example.entity.department.DepartmentTreeBO;
import com.example.service.IDepartmentService;
import com.example.utils.RedisListOrder;
import com.example.utils.RedisUtil;
import com.example.utils.TestOrder;
import com.example.utils.redistemplate.RedisTemplateUtils;
import org.redisson.Redisson;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ChenXiaoBin
 * on 2019-07-04
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<IDepartmentDao, Department> implements IDepartmentService {

    @Autowired
    private IDepartmentDao departmentDao;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedisTemplateUtils redisTemplateUtils;


    /**
     * 递归查询部门树列表
     */
    @Override
//    @Cacheable(value = "index_home_user_sex#600", key = "#parentId")
    public List<DepartmentTreeBO> getDepartmentList(Integer parentId) {
        //获取第一级部门
        List<DepartmentTreeBO> departTree = departmentDao.getDepartment(parentId);
        if (departTree.size() > 0) {
            for (DepartmentTreeBO departmentTreeBO : departTree) {
                List<DepartmentTreeBO> chile = getDepartmentList(departmentTreeBO.getId());
                departmentTreeBO.setChildren(chile);
            }
        }
        return departTree;
    }

    @Override
    public List<Department> getDepartmentByDate(String date) {
        return departmentDao.getDepartmentByDate(date);
    }

    /**
     * 测试redis做缓存
     * @return
     */
    @Override
    public List<Department> departmentList() {
        String key = "department_";
        boolean hasKey = RedisUtil.hasKey(key);
        if (hasKey) {
             String departments = RedisUtil.get(key);
            return null;
        } else {
            List<Department> departmentList = departmentDao.departmentList();
            RedisUtil.set(key, departmentList.toString(), 5);
            return departmentList;
        }

    }

    @Override
    public void delay() {
//        RedissonClient redissonClient = Redisson.create(config);
        RBlockingQueue<CallCdr> blockingFairQueue = redissonClient.getBlockingQueue("delay_queue");
        RDelayedQueue<CallCdr> delayedQueue = redissonClient.getDelayedQueue(blockingFairQueue);

        CallCdr callCdr = new CallCdr();
        callCdr.setPutTime();
        callCdr.setName("书本");
        System.out.println("1332313");
        delayedQueue.offer(callCdr, 10L, TimeUnit.SECONDS);
        //在该对象不再需要的情况下，应该主动销毁。
        //仅在相关的Redisson对象也需要关闭的时候可以不用主动销毁。
        delayedQueue.destroy();

        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("fdfdfdfdfdddd");

    }

    @Override
    public DepartmentOneBO getCreateTime() {

        DepartmentOneBO createTime = departmentDao.getCreateTime();
        return null;
    }

    // @PostConstruct：当整个bean被初始化完成后执行
    @PostConstruct
    public void init() {
        Thread takeOrder = new Thread(new TestOrder(redissonClient, redisTemplateUtils));
        takeOrder.start();
        Thread redisList = new Thread(new RedisListOrder(redisTemplateUtils));
        redisList.start();
//        config.useSingleServer().setAddress("redis://117.50.75.80:8888").setPassword("hKTM0xe7E3").setDatabase(0);
    }

    // 销毁示例之前调用
    @PreDestroy
    public void close() {
        Thread takeOrder = new Thread(new TestOrder(redissonClient, redisTemplateUtils));
        takeOrder.interrupt();

        Thread redisList = new Thread(new RedisListOrder(redisTemplateUtils));
        redisList.interrupt();
    }
}
